package com.wanxi.controller;

import com.wanxi.VO.UserVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/3/9 16:17
 */
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping ("/back/login")
    protected ResultDTO loginRegister(UserVO userVO, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResultDTO resultDTO;
        userVO.setSessionCode((String) req.getSession().getAttribute("vCode"));
        Object attribute = req.getServletContext().getAttribute("username");
        if (attribute != null) {
            resultDTO = new ResultDTO(-1,"账号已在别处登录！",0,null);
            return resultDTO;
        }else {
            //5.得到返回结果
            resultDTO = userService.login(userVO);
            if ("success".equals(resultDTO.getMsg())) {
                req.getSession().setAttribute("uu", userVO.getUsername());
                req.getServletContext().setAttribute("uu", userVO.getUsername());
            }
        }
        return resultDTO;
    }

}
